using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System;
using System.Reflection;
using System.Security.Permissions;
using Components2.Sharepoint;

namespace Components2.CustomAdmin {

    public class CustomMasterPageReceiverSPWebConfigModification : AbstractSPWebConfigModificationSPFeatureReceiverTask {

        protected override void AddConfigurationToWebConfig(SPWebApplication app) {
            string fullName = typeof(CustomMasterPageModule).AssemblyQualifiedName;
            AddNodeValue(
                "add[@name='CustomMasterPage']", //case sensitive
                "configuration/system.web/httpModules",
                string.Format("<add name=\"CustomMasterPage\" type=\"{0}\" />", fullName));
            SaveWebConfig(app);
        }

        protected override string OwnerModif {
            get { return "Components2.CustomAdmin.CustomMasterPageReceiverSPWebConfigModification"; }
        }
    }

    public class CustomMasterPageReceiver : SPFeatureReceiver {

        private CustomMasterPageReceiverSPWebConfigModification task1 = new CustomMasterPageReceiverSPWebConfigModification();

        public override void FeatureInstalled(SPFeatureReceiverProperties properties) {

        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties) {
            SPWebApplication webapp = properties.Feature.Parent as SPWebApplication;
            if (webapp is SPAdministrationWebApplication) {
                throw new SPException("Cannot enable this feature on administration web applications");
            } else {
                task1.AddOrRemoveHandler(properties, false);
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
            task1.AddOrRemoveHandler(properties, true);
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties) {

        }
    }
}